package org.llw.ldailytask.util;

import java.util.List;

import org.llw.ldailytask.R;
import org.llw.ldailytask.dao.SqliteDao;
import org.llw.ldailytask.entity.Task;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.roamer.slidelistview.SlideBaseAdapter;
import com.roamer.slidelistview.SlideListView.SlideMode;

public class SlideAdapter extends SlideBaseAdapter {
	private List<Task> mData;
	private CallBack callBack;
	private Context mcontext;

	public SlideAdapter(Context context, List<Task> data) {
		super(context);
		mData = data;
		this.mcontext=context;
		callBack=(CallBack) context;
	}

	
	@Override
	public SlideMode getSlideModeInPosition(int position) {
//		if (position == 1) {
//			return SlideMode.LEFT;
//		}
//		if (position == 2) {
//			return SlideMode.NONE;
//		}
//		return super.getSlideModeInPosition(position);
		return SlideMode.BOTH;
	}

	@Override
	public int getFrontViewId(int position) {
		return R.layout.row_front_view2;
		//return R.layout.row_front_view;
		
		
	}

	@Override
	public int getLeftBackViewId(int position) {
//		if (position % 2 == 0) {
//			return R.layout.row_left_back_view;
//		}
		return R.layout.row_left_back_view;
	}

	@Override
	public int getRightBackViewId(int position) {
		return R.layout.row_right_back_view;
	}

	@Override
	public int getItemViewType(int position) {
		if (position % 2 == 0) {
			return 0;
		}
		return 1;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getCount() {
		System.out.println("getCount()=  "+ mData.size());
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = createConvertView(position);
			holder = new ViewHolder();
			//holder.title = (Button) convertView.findViewById(R.id.title);
			holder.edit = (Button) convertView.findViewById(R.id.edit);
			holder.delete = (Button) convertView.findViewById(R.id.delete);
			holder.detail = (Button) convertView.findViewById(R.id.detail);
			
			holder.isalert=(TextView) convertView.findViewById(R.id.isalert);
			holder.date=(TextView) convertView.findViewById(R.id.date);
			holder.status=(TextView) convertView.findViewById(R.id.status);
			holder.type=(TextView) convertView.findViewById(R.id.type);
			holder.taskID=(TextView) convertView.findViewById(R.id.taskID);
			holder.txtTime=(TextView) convertView.findViewById(R.id.tvTime);
			holder.txtListTitle =  (TextView) convertView.findViewById(R.id.tvTitle);
			holder.txtLIstContent = (TextView) convertView.findViewById(R.id.tvContent);
			holder.imgTaskType = (ImageView) convertView.findViewById(R.id.imgTaskIcon);
			holder.txtListDel=(TextView) convertView.findViewById(R.id.txtRowRightDel);
			holder.txtListLeft=(TextView) convertView.findViewById(R.id.txtRowLeft);
			holder.listItem= (RelativeLayout) convertView.findViewById(R.id.front);
			//在给List添加每行item的时候，添加 按日期查询数据库返回的数据 ，并根据 紧急度 完成度 设置样式
			//holder.listItem.setBackgroundResource(R.color.color_calendar_weekend);
			holder.imgTaskType.setImageResource(R.drawable.img_tasktype_1);
			
			holder.isalert.setText(mData.get(position).getIsAlert().toString());
			holder.date.setText(mData.get(position).getDate().toString());
			holder.status.setText(mData.get(position).getStatus().toString());
			holder.type.setText(mData.get(position).getType().toString());
			holder.taskID.setText(String.valueOf(mData.get(position).getId()));
			holder.txtTime.setText(mData.get(position).getTime().toString());
			holder.txtListTitle.setText(mData.get(position).getTitle().toString());
			holder.txtLIstContent.setText(mData.get(position).getContent().toString());
			
			
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

//		String text = mData.get(position);
//		holder.title.setText(text);
//		holder.title.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Toast.makeText(mContext, "Click title:" + position, Toast.LENGTH_SHORT).show();
//			}
//		});

		if (holder.txtListLeft!= null) {
			holder.txtListLeft.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(mContext, "标记功能正在开发中...", Toast.LENGTH_SHORT).show();
				}
			});
		}

		if (holder.txtListDel != null) {
			holder.txtListDel.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					SqliteDao sqliteDao= new SqliteDao(mcontext);
					sqliteDao.delTask(mData.get(position).getId());
					mData.remove(position);
					notifyDataSetChanged();
				}
			});
		}

		if (holder.detail != null) {
			holder.detail.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(mContext, "Click detail:" + position, Toast.LENGTH_SHORT).show();
				}
			});
		}
		return convertView;
	}

	public List<Task> getmData() {
		return mData;
	}


	public void setmData(List<Task> mData) {
		this.mData = mData;
	}

	class ViewHolder {
		RelativeLayout listItem;
		TextView txtListTitle, txtLIstContent,txtListMark,txtListDel,txtListLeft,txtTime,taskID,isalert,type,status,date;
		ImageView imgTaskType;
		Button title;
		Button edit;
		Button delete;
		Button detail;
	}

}